package com.hejia.bulbul.controller;

import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.hejia.alauda.entity.Fund;
import com.hejia.alauda.entity.SaleActivity;
import com.hejia.alauda.service.FundService;
import com.hejia.alauda.service.SaleActivityService;
import com.hejia.alauda.utils.Pager;

@Controller
@RequestMapping("fundmanage")
public class FundManageController extends BaseController {

	@Resource
	private FundService fundService;

	@Resource
	private SaleActivityService saleActivityService;

	@RequestMapping(value = "fundmanagelist", method = RequestMethod.GET)
	public String FundManageList(ModelMap model, Pager<Fund> pager) {
		Pager<Fund> funds = fundService.findAllFund(pager);
		model.put("funds", funds);
		return "/product/fundmanage/fundmanage_list";
	}

	/**
	 * 跳转到添加界面
	 */
	@RequestMapping(value = "fundmanageadd", method = RequestMethod.GET)
	public String addFundManage(ModelMap model) {
		// 获取活动
		List<SaleActivity> saList = saleActivityService.effectiveActivity();
		model.put("saleActivityList", saList);
		return "/product/fundmanage/fundmanage_input";
	}

	/**
	 * 添加完保存
	 */
	@RequestMapping(value = "fundmanageadd", method = RequestMethod.POST)
	public String saveFundManage(Fund fund, ModelMap model,
			RedirectAttributes redirectAttributes) {
		if (fundService.saveFund(fund)) {
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "基金管理添加成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "基金管理添加失败");
		}

		return "redirect:/fundmanage/fundmanagelist";
	}

	/**
	 * 跳转到修改界面
	 */
	@RequestMapping(value = "fundmanageupdate/{id}", method = RequestMethod.GET)
	public String updateFundManage(@PathVariable Long id, ModelMap model) {
		Fund fund = fundService.getFundById(id);
		// 获取活动
		List<SaleActivity> saList = saleActivityService.effectiveActivity();
		model.put("saleActivityList", saList);
		model.put("fund", fund);
		return "/product/fundmanage/fundmanage_input";
	}

	/**
	 * 修改完保存
	 */
	@RequestMapping(value = "fundmanageupdate/{id}", method = RequestMethod.POST)
	public String saveUpdateFundManage(Fund fund,
			RedirectAttributes redirectAttributes) {
		if (fundService.modifyFund(fund)) {
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "基金管理修改成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "基金管理修改失败");
		}

		return "redirect:/fundmanage/fundmanagelist";
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "fundmanagedelete/{id}", method = RequestMethod.GET)
	public String deleteFundManage(@PathVariable Long id,
			RedirectAttributes redirectAttributes) {
		if (fundService.deleteFund(id)) {
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "基金管理删除成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "基金管理删除失败");
		}

		return "redirect:/fundmanage/fundmanagelist";
	}

	/**
	 * 上架
	 */
	@RequestMapping(value = "up/{id}", method = RequestMethod.GET)
	public String up(@PathVariable Long id,
			RedirectAttributes redirectAttributes) {
		if (fundService.upFund(id)) {
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "上架成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "上架失败");
		}
		return "redirect:/fundmanage/fundmanagelist";
	}

	/**
	 * 下架
	 */
	@RequestMapping(value = "down/{id}", method = RequestMethod.GET)
	public String down(@PathVariable Long id,
			RedirectAttributes redirectAttributes) {
		if (fundService.downFund(id)) {
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "下架成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "下架失败");
		}
		return "redirect:/fundmanage/fundmanagelist";
	}

	/**
	 * 暂停销售
	 */
	@RequestMapping(value = "stop/{id}", method = RequestMethod.GET)
	public String stop(@PathVariable Long id,
			RedirectAttributes redirectAttributes) {
		if (fundService.stopFund(id)) {
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "暂停销售成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "暂停销售失败");
		}
		return "redirect:/fundmanage/fundmanagelist";
	}

}
